using System;
using Csla;
using Ent.AccesoDatos;
using System.Collections.Generic;

namespace Ent.Inscripcion.Core.Administracion
{
    [Serializable]
    public class HorarioTutoria : BusinessBase<HorarioTutoria>
    {
        #region Business Methods

        private static PropertyInfo<Guid> IdProperty = RegisterProperty<Guid>(o => o.Id, "Id");
        public Guid Id
        {
            get { return GetProperty(IdProperty); }
            set { SetProperty(IdProperty, value); }
        }

        private static PropertyInfo<DateTime> HoraInicioProperty = RegisterProperty<DateTime>(o => o.HoraInicio, "Hora Inicio");
        public DateTime HoraInicio
        {
            get { return GetProperty(HoraInicioProperty); }
            set { SetProperty(HoraInicioProperty, value); }
        }

        private static PropertyInfo<DateTime> HoraFinProperty = RegisterProperty<DateTime>(o => o.HoraFin, "Hora Fin");
        public DateTime HoraFin
        {
            get { return GetProperty(HoraFinProperty); }
            set { SetProperty(HoraFinProperty, value); }
        }

        private static PropertyInfo<string> AulaIdProperty = RegisterProperty<string>(o => o.AulaId, "Name");
        public string AulaId
        {
            get { return GetProperty(AulaIdProperty); }
            set { SetProperty(AulaIdProperty, value); }
        }

        private static PropertyInfo<string> NivelIdProperty = RegisterProperty<string>(o => o.NivelId, "Name");
        public string NivelId
        {
            get { return GetProperty(NivelIdProperty); }
            set { SetProperty(NivelIdProperty, value); }
        }

        private static PropertyInfo<string> DiaIdProperty = RegisterProperty<string>(o => o.DiaId, "Name");
        public string DiaId
        {
            get { return GetProperty(DiaIdProperty); }
            set { SetProperty(DiaIdProperty, value); }
        }

        private static PropertyInfo<string> ParaleloIdProperty = RegisterProperty<string>(o => o.ParaleloId, "Name");
        public string ParaleloId
        {
            get { return GetProperty(ParaleloIdProperty); }
            set { SetProperty(ParaleloIdProperty, value); }
        }

        private static PropertyInfo<string> DocenteIdProperty = RegisterProperty<string>(o => o.DocenteId, "Name");
        public string DocenteId
        {
            get { return GetProperty(DocenteIdProperty); }
            set { SetProperty(DocenteIdProperty, value); }
        }

        private static PropertyInfo<string> OfertaTutoriaIdProperty = RegisterProperty<string>(o => o.OfertaTutoriaId, "Name");
        public string OfertaTutoriaId
        {
            get { return GetProperty(OfertaTutoriaIdProperty); }
            set { SetProperty(OfertaTutoriaIdProperty, value); }
        }       

        private static PropertyInfo<int> EstadoProperty = RegisterProperty<int>(o => o.Estado, "Name");
        public int Estado
        {
            get { return GetProperty(EstadoProperty); }
            set { SetProperty(EstadoProperty, value); }
        }

        private static PropertyInfo<int> CupoProperty = RegisterProperty<int>(o => o.Cupo, "Name");
        public int Cupo
        {
            get { return GetProperty(CupoProperty); }
            set { SetProperty(CupoProperty, value); }
        }
        #endregion

        #region Validation Rules

        protected override void AddBusinessRules()
        {
            // TODO: add validation rules
            //ValidationRules.AddRule(RuleMethod, "");
        }

        #endregion

        #region Factory Methods

        public static HorarioTutoria NewHorarioTutoria()
        {
            return DataPortal.Create<HorarioTutoria>();
        }

        public static HorarioTutoria GetHorarioTutoria(Guid id)
        {
            return DataPortal.Fetch<HorarioTutoria>(id);
        }

        public static void DeleteHorarioTutoria(Guid id)
        {
            DataPortal.Delete<HorarioTutoria>(id);
        }

        private HorarioTutoria()
        { LoadProperty(IdProperty, Guid.NewGuid()); }

        #endregion

        #region Data Access

        [RunLocal]
        protected override void DataPortal_Create()
        {
            // TODO: load default values
            // omit this override if you have no defaults to set
            base.DataPortal_Create();
        }

        private void DataPortal_Fetch(Guid criteria)
        {
            var accesoDatos = new DriveDataBase(TypeDataBase.Mysql);
            var parametros = new List<Parametro> {new Parametro("Id", criteria)};
            var datos = accesoDatos.EjecutarReader("ConsultarHorarioTutoria", parametros);
            if (datos.Datos.Read())
            {
                LoadProperty(IdProperty, Guid.Parse(datos.Datos.GetString(0)));
                LoadProperty(HoraInicioProperty, DateTime.Parse(datos.Datos.GetString(1)));
                LoadProperty(HoraFinProperty, DateTime.Parse(datos.Datos.GetString(2)));
                LoadProperty(AulaIdProperty, datos.Datos.GetString(3));
                LoadProperty(NivelIdProperty, datos.Datos.GetString(4));
                LoadProperty(DiaIdProperty, datos.Datos.GetString(5));
                LoadProperty(ParaleloIdProperty, datos.Datos.GetString(6));
                LoadProperty(OfertaTutoriaIdProperty, datos.Datos.GetString(7));
                LoadProperty(DocenteIdProperty, datos.Datos.GetString(8));
                LoadProperty(EstadoProperty, datos.Datos.GetInt32(9));
                LoadProperty(CupoProperty, datos.Datos.GetInt32(10));
            }
            if (!datos.Datos.IsClosed) datos.Datos.Close();
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            var accesoDatos = new DriveDataBase(TypeDataBase.Mysql);
            List<Parametro> parametros = new List<Parametro>();
            parametros.Add(new Parametro("Hora_Inicio", ReadProperty(HoraInicioProperty)));
            parametros.Add(new Parametro("Hora_Fin", ReadProperty(HoraFinProperty)));
            parametros.Add(new Parametro("Aula", ReadProperty(AulaIdProperty)));
            parametros.Add(new Parametro("Nivel", ReadProperty(NivelIdProperty)));
            parametros.Add(new Parametro("Dia", ReadProperty(DiaIdProperty)));
            parametros.Add(new Parametro("Estado", ReadProperty(EstadoProperty)));
            parametros.Add(new Parametro("Paralelo", ReadProperty(ParaleloIdProperty)));
            parametros.Add(new Parametro("Oferta", ReadProperty(OfertaTutoriaIdProperty)));
            parametros.Add(new Parametro("Docente", ReadProperty(DocenteIdProperty)));
            parametros.Add(new Parametro("Cupo", ReadProperty(CupoProperty)));
            parametros.Add(new Parametro("Id", ReadProperty(IdProperty)));
            accesoDatos.EjecutarQuery("InsertarHorarioTutoria", parametros);
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            var accesoDatos = new DriveDataBase(TypeDataBase.Mysql);
            List<Parametro> parametros = new List<Parametro>();
            parametros.Add(new Parametro("Hora_Inicio", ReadProperty(HoraInicioProperty)));
            parametros.Add(new Parametro("Hora_Fin", ReadProperty(HoraFinProperty)));
            parametros.Add(new Parametro("Aula", ReadProperty(AulaIdProperty)));
            parametros.Add(new Parametro("Nivel", ReadProperty(NivelIdProperty)));
            parametros.Add(new Parametro("Dia", ReadProperty(DiaIdProperty)));
            parametros.Add(new Parametro("Estado", ReadProperty(EstadoProperty)));
            parametros.Add(new Parametro("Paralelo", ReadProperty(ParaleloIdProperty)));
            parametros.Add(new Parametro("Oferta", ReadProperty(OfertaTutoriaIdProperty)));
            parametros.Add(new Parametro("Docente", ReadProperty(DocenteIdProperty)));
            parametros.Add(new Parametro("Cupo", ReadProperty(CupoProperty)));
            parametros.Add(new Parametro("Id", ReadProperty(IdProperty)));
            accesoDatos.EjecutarQuery("ActualizarHorarioTutoria", parametros);
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(ReadProperty(IdProperty));
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        private void DataPortal_Delete(Guid criteria)
        {
            var accesoDatos = new DriveDataBase(TypeDataBase.Mysql);
            List<Parametro> parametros = new List<Parametro>();
            parametros.Add(new Parametro("Id", ReadProperty(IdProperty)));
            accesoDatos.EjecutarQuery("EliminarHorarioTutoria", parametros);
        }

        #endregion
    }
}
